<?php
class usersActions extends sfGraduaActions
{

    public function executeIndex($request)
    {
        $this->users = UsersService::getAll();
        $roles = RolesService::getAllCategorized(false);
        $this->generalRoles = $roles['general'];
        $projects = ProjectsService::getAll(false);
        $this->projectRoles = array();
        // cria um array com as regras específicas de projeto, para cada projeto, para gerar o tpl.
        foreach($projects as $project) {
            $ar = array();
            $ar['roles'] = $roles['project'];
            $ar['project'] = $project;
            $this->projectRoles[] = $ar;
        }
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->userViewTpl  = partial2Js('users/userView.jstpl',array(),true);
        $this->userFormTpl  = partial2Js('users/userForm.jstpl',array(),true);
        $this->userDetailsTpl  = partial2Js('users/userDetails.jstpl',array(),true);
        $this->editDecorator   = partial2Js('globalPartials/editDecorator.jstpl',array(),true);
        $this->insertDecorator = partial2Js('globalPartials/insertDecorator.jstpl',array(),true);
        $this->viewDecorator   = partial2Js('globalPartials/viewDecoratorOver.jstpl',array(),true);
        $this->listTpl         = partial2Js('globalPartials/listTpl.jstpl',array(),true);
        $this->divListItemDecorator   = partial2Js('globalPartials/divListItemDecorator.jstpl',array(),true);
        $this->setLayout('layoutTasks');
    }

    function executeInsert($request) {
        $data = $this->getObjRequest('user');
        $jsObjName = $this->getRequestParameter('jsObjName');
        $this->getResponse()->setTransferErrorFieldNames(array(
          'name','password'
        ));
        $data->generalRoles = isset($data->generalRoles) ? $data->generalRoles : array();
        if(isset($data->projectRoles)) {
            $arCleanedData = array();
            foreach($data->projectRoles as $projectId=>$roleIds) {
                foreach($roleIds as $roleId) {
                   $ar = array();
                   $ar['project_id'] = $projectId;
                   $ar['role_id'] = $roleId;
                   $arCleanedData[] = $ar;
                }
            }
            $data->projectRoles = $arCleanedData;
        }
        else {
            $data->projectRoles = array();
        }
        $this->getResponse()->setFieldIdPrefix('user');
        $id = UsersService::insert($data);
        $this->getResponseJsCall()
            ->addCommand('users.list', 'refresh')
            ->addPHPParam('itemsDs', UsersService::getAll() )
            ->closeDialog()
            ->showFeedback('Inserido com sucesso!');
        return sfView::NONE;
    }
    
    function executeEdit($request) {
        //Dbg::x($this->getRequest()->getParameterHolder());
        $data = $this->getObjRequest('user',array('use_tests'));
        $jsObjName = $this->getRequestParameter('jsObjName');
        $this->getResponse()->setTransferErrorFieldNames(array(
          'name','password'
        ));
        $data->generalRoles = isset($data->generalRoles) ? $data->generalRoles : array();
        if(isset($data->projectRoles)) {
            $arCleanedData = array();
            foreach($data->projectRoles as $projectId=>$roleIds) {
                foreach($roleIds as $roleId) {
                   $ar = array();
                   $ar['project_id'] = $projectId;
                   $ar['role_id'] = $roleId;
                   $arCleanedData[] = $ar;
                }
            }
            $data->projectRoles = $arCleanedData;
        }
        else {
            $data->projectRoles = array();
        }
        $this->getResponse()->setFieldIdPrefix('project');
        //$('#projectForm-1 [name="project[name]"]').val('*ooo*')
        UsersService::edit($data);
        $this->getResponseJsCall()
            ->addCommand($jsObjName, 'update')
            ->addPHPParam('ds', UsersService::get($data->id))
            ->showFeedback('Sucesso na edição do Usuário!');
        return sfView::NONE;
    }

    function executeRemove($request) {
        //Dbg::x($this->getRequest()->getParameterHolder());
        $id = $this->getRequestParameter('id');
        $jsObjName = $this->getRequestParameter('jsObjName');
        UsersService::delete($id);
        $this->getResponseJsCall()
            ->addCommand('users.list', 'refresh')
            ->addPHPParam('itemsDs', UsersService::getAll() )
            ->closeDialog()
            ->showFeedback('Removido com sucesso!');
        return sfView::NONE;
    }
    
}
